package com.thirdlib.timeline;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.lookRepair.activity.LookForRepairDetailActivity;
import com.orangelife.mobile.lookRepair.activity.PlaceOrderDetailActivity;
import com.orangelife.mobile.lookRepair.activity.PricingAndValuationActivity;
import com.orangelife.mobile.lookRepair.activity.RepairProblemActivity;
import com.orangelife.mobile.util.DateUtil;

import java.util.List;
import java.util.Map;

public class TimelineAdapter extends BaseAdapter {

	private Context context;
	private List<Map<String, Object>> list;
	private LayoutInflater inflater;
	private int position;
	ViewHolder viewHolder = null;
	private String invcRepaireID;
	private int count = 0;
	private int count2 = 0;
	private int status = 0;
	private String repairCode;

	public TimelineAdapter(Context context, List<Map<String, Object>> list,
			String invcRepaireID,String repairCode) {
		super();
		this.context = context;
		this.list = list;
		isVis(list);
		this.invcRepaireID = invcRepaireID;
		this.repairCode = repairCode;
	}

	private void isVis(List<Map<String, Object>> list) {
		for (int i = 0; i < list.size(); i++) {
			String followType = list.get(i).get("followType").toString();
			if (followType.equals("4") || followType.equals("5")) {
				count = i;
			}
			if (followType.equals("7") || followType.equals("8")) {
				count2 = i;
			}
		}
	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater
					.inflate(R.layout.item_lv_repair_detail, null);
			viewHolder = new ViewHolder();
			viewHolder.viewHorizontal = (View) convertView
					.findViewById(R.id.viewHorizontal);
			viewHolder.viewVertical = (View) convertView
					.findViewById(R.id.viewVertical);
			viewHolder.tvTitle = (TextView) convertView
					.findViewById(R.id.tvTitle);
			viewHolder.btnClick = (Button) convertView
					.findViewById(R.id.btnClick);
			viewHolder.ivProgress = (ImageView) convertView
					.findViewById(R.id.iv_Progress);
			viewHolder.viewVertical2 = (View) convertView
					.findViewById(R.id.viewVertical2);
			viewHolder.llRight = (LinearLayout) convertView
					.findViewById(R.id.llRight);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		this.position = position;
		DateUtil date = new DateUtil();
		String title = "";
		Map<String, Object> map = (Map<String, Object>) list.get(position);
		status = Integer.parseInt(map.get("followType").toString());
		if (status == 2) {
			title = "抢单:";
		} else if (status == 3) {
			title = "上门:";
		} else if (status == 4) {
			title = "估价:";
		} else if (status == 5) {
			title = "拒绝估价:";
		} else if (status == 6) {
			title = "跟进:";
		} else if (status == 7) {
			title = "定价验收:";
		} else if (status == 8) {
			title = "拒绝定价:";
		} else if (status == 9) {
			title = "保修申请:";
		} else if (status == 10) {
			title = "结单:";
		} else if (status == 11) {
			title = "取消订单:";
		} else if (status == 12) {
			title = "确认估价:";
		} else if (status == 13) {
			title = "确认定价:";
		} else if (status == 21) {
			title = "重新下单:";
		}else if(status == 1){
			title = "下单：";
		}
		if (list.size() == 1) {
			viewHolder.viewVertical.setVisibility(View.INVISIBLE);
			viewHolder.viewVertical2.setVisibility(View.VISIBLE);
			viewHolder.ivProgress
					.setImageResource(R.drawable.progress_orange_small);
			viewHolder.tvTitle.setTextSize(15);
			viewHolder.tvTitle.setTextColor(context.getResources().getColor(
					R.color.darkOrange));
			viewHolder.viewVertical2.setVisibility(View.GONE);
			viewHolder.tvTitle.setText(title
					+ date.tranStringForDate_11(map.get("tsedit").toString()));
			viewHolder.btnClick.setVisibility(View.GONE);
			viewHolder.viewHorizontal.setVisibility(View.GONE);
		} else {
			if (position == 0) {
				viewHolder.viewVertical.setVisibility(View.INVISIBLE);
				viewHolder.viewVertical2.setVisibility(View.VISIBLE);
				viewHolder.ivProgress
						.setImageResource(R.drawable.progress_gray_small);
				viewHolder.tvTitle.setTextSize(12);
				viewHolder.tvTitle.setTextColor(context.getResources()
						.getColor(R.color.DarkGray));
				viewHolder.btnClick.setVisibility(View.GONE);
				// LinearLayout.LayoutParams lp1 = new
				// LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				// 13);
				// viewHolder.viewVertical2.setLayoutParams(lp1);
				viewHolder.tvTitle.setText(title
						+ date.tranStringForDate_11(map.get("tsedit")
								.toString()));
				viewHolder.viewHorizontal.setVisibility(View.VISIBLE);

			} else if (position == list.size() - 1) {
				viewHolder.ivProgress
						.setImageResource(R.drawable.progress_orange_small);
				viewHolder.tvTitle.setTextSize(15);
				viewHolder.tvTitle.setTextColor(context.getResources()
						.getColor(R.color.darkOrange));
				viewHolder.viewVertical2.setVisibility(View.GONE);
				viewHolder.tvTitle.setText(title
						+ date.tranStringForDate_11(map.get("tsedit")
								.toString()));
				viewHolder.viewHorizontal.setVisibility(View.GONE);
				if (status == 3) {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setText("师傅上门记录");
				} else if (status == 4 || status == 5) {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setText("查看估价单");
				} else if (status == 6) {
					viewHolder.llRight.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setVisibility(View.GONE);
				} else if (status == 7 || status == 8) {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setText("查看定价单");
				} else {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.GONE);
				}
				

			} else {
				viewHolder.viewVertical2.setVisibility(View.VISIBLE);
				// LinearLayout.LayoutParams lp = new
				// LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				// LinearLayout.LayoutParams.MATCH_PARENT);
				// viewHolder.viewVertical2.setLayoutParams(lp);
				viewHolder.viewVertical.setVisibility(View.VISIBLE);
				viewHolder.tvTitle.setTextColor(context.getResources()
						.getColor(R.color.DarkGray));
				viewHolder.tvTitle.setText(title
						+ date.tranStringForDate_11(map.get("tsedit")
								.toString()));
				viewHolder.viewHorizontal.setVisibility(View.VISIBLE);
				// 当有拒绝估价单的时候，是否每次都要显示不同的估价单。
				int stat = Integer.parseInt(map.get("followType").toString());
				if (status == 3) {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setText("师傅上门记录");
				} else if (status == 4 || status == 5) {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setText("查看估价单");
				} else if (status == 6) {
					viewHolder.llRight.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setVisibility(View.GONE);
				} else if (status == 7 || status == 8) {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.VISIBLE);
					viewHolder.btnClick.setText("查看定价单");
				} else {
					viewHolder.llRight.setVisibility(View.GONE);
					viewHolder.btnClick.setVisibility(View.GONE);
				}
			}
		}
		if(position != (list.size() - 1)){
			if (count != position
					&& (status==4 || status==5)) {
				viewHolder.btnClick.setVisibility(View.GONE);
			}

			if (count2 != position && (status==7 || status==8)){
				viewHolder.btnClick.setVisibility(View.GONE);
			}
		}
		
		viewHolder.btnClick.setOnClickListener(new listener(map));
		viewHolder.llRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Intent intent = new Intent();
				String followID = list.get(position).get("followID").toString();
				if(position == 0){
					intent.putExtra("repairCode", repairCode);
					intent.setClass(context, PlaceOrderDetailActivity.class);
				}else{
					intent.putExtra("lookDetail", 2);
					intent.putExtra("followID", followID);
					intent.setClass(context, RepairProblemActivity.class);
				}
				context.startActivity(intent);
			}
		});

		return convertView;
	}

	/**
	 * 监听器
	 * 
	 * @author YYX
	 * 
	 */
	class listener implements View.OnClickListener {

		private Map<String, Object> map = null;

		public listener(Map<String, Object> map) {
			this.map = map;
		}

		@Override
		public void onClick(View view) {
			Intent intent = new Intent();
			String followID = map.get("followID").toString();
			intent.putExtra("invcRepaireID", invcRepaireID);
			switch (view.getId()) {
//			case R.id.llRight:
//				if(position == 0){
//					intent.putExtra("repairCode", repairCode);
//					intent.setClass(context, PlaceOrderDetailActivity.class);
//				}else{
//					intent.putExtra("lookDetail", 2);
//					intent.putExtra("followID", followID);
//					intent.setClass(context, RepairProblemActivity.class);
//				}
//				context.startActivity(intent);
//				break;
			case R.id.btnClick:
				int stat = Integer.parseInt(map.get("followType").toString());
				if (stat == 3) {
					ExitApplication.getInstance().addPaidActivity(
							(LookForRepairDetailActivity) context);
					intent.putExtra("lookDetail", 1);
					intent.putExtra("followID", followID);
					intent.setClass(context, RepairProblemActivity.class);
					context.startActivity(intent);
				} else if (stat == 4 || stat == 5) {
					ExitApplication.getInstance().addPaidActivity(
							(LookForRepairDetailActivity) context);
					if(count2 == 0){
						intent.putExtra("isNeed", 0);
					}else{
						intent.putExtra("isNeed", 1);
					}
					intent.putExtra("VP", 0);
					intent.setClass(context, PricingAndValuationActivity.class);
					context.startActivity(intent);
				} else if (stat == 7 || stat == 8) {
					ExitApplication.getInstance().addPaidActivity(
							(LookForRepairDetailActivity) context);
					if(count2 == 0){
						intent.putExtra("isNeed", 0);
					}else{
						intent.putExtra("isNeed", 1);
					}
					intent.putExtra("VP", 1);
					intent.setClass(context, PricingAndValuationActivity.class);
					context.startActivity(intent);
				}

				break;

			default:
				break;
			}

		}
	}

	static class ViewHolder {
		/** 圆点上面的竖条 */
		private View viewVertical;
		/** 圆点下面的竖条 */
		private View viewVertical2;
		/** 圆点右边的字 */
		private TextView tvTitle;
		/** 查看按钮 */
		private Button btnClick;
		/** 圆点 */
		private ImageView ivProgress;
		/** 查看跟进中 */
		private LinearLayout llRight;
		/** 横向view */
		private View viewHorizontal;
	}
}
